﻿// <copyright file="RepeatedSwitchNTelegram.cs" company="Hallvard Korsgaard">
//  Microsoft Public License (Ms-PL). See license details here: http://enoceannet.codeplex.com/license
// </copyright>
// <author>Hallvard Korsgaard</author>

namespace EnOcean.Radio.Sensor
{
  using System;

  /// <summary>
  /// N-message from a switch module (ORG = 0x05).
  /// </summary>
  [Serializable]
  public class RepeatedSwitchNTelegram : RepeatedSwitchTelegram
  {
    /// <summary>
    /// Variable for the FirstButtonId property. 
    /// </summary>
    private byte firstButtonId;

    /// <summary>
    /// Variable for the SecondButtonId property. 
    /// </summary>
    private byte secondButtonId;

    /// <summary>
    /// Gets or sets the first button ID.
    /// </summary>
    public byte FirstButtonId
    {
      get
      {
        return this.firstButtonId;
      }

      set
      {
        if (value >= 0x0 && value <= 0x3)
        {
          this.firstButtonId = value;
        }
        else
        {
          throw new ArgumentOutOfRangeException("FirstButtonId", "must be a 2 bit value in the range 0x0 to 0x3 inclusive");
        }
      }
    }

    /// <summary>
    /// Gets or sets the first button state.
    /// </summary>
    public ButtonState FirstButtonState
    {
      get;
      set;
    }

    /// <summary>
    /// Gets or sets the second button ID.
    /// </summary>
    public byte SecondButtonId
    {
      get
      {
        return this.secondButtonId;
      }

      set
      {
        if (value >= 0x0 && value <= 0x3)
        {
          this.secondButtonId = value;
        }
        else
        {
          throw new ArgumentOutOfRangeException("SecondButtonId", "must be a 2 bit value in the range 0x0 to 0x3 inclusive");
        }
      }
    }

    /// <summary>
    /// Gets or sets the second button state.
    /// </summary>
    public ButtonState SecondButtonState
    {
      get;
      set;
    }

    /// <summary>
    /// Gets or sets a value indicating whether the second action is in use, i.e 2 buttons pressed simultaneously.
    /// </summary>
    public bool HasSecondAction
    {
      get;
      set;
    }

    /// <summary>
    /// Creates the byte format of the message.
    /// </summary>
    /// <param name="message">A byte array of length 14 to hold the message contents.</param>
    protected internal override void CreateMessage(byte[] message)
    {
      base.CreateMessage(message);

      // Data 3
      message[4] = 0;
      if (this.HasSecondAction)
      {
        message[4] = 1;
      }

      if (this.SecondButtonState == ButtonState.O)
      {
        message[4] |= 0x02;
      }

      message[4] |= (byte)((uint)this.SecondButtonId << 2);

      if (IsEnergyBowPressed)
      {
        message[4] |= 0x10;
      }

      if (this.FirstButtonState == ButtonState.O)
      {
        message[4] |= 0x20;
      }

      message[4] |= (byte)((uint)this.FirstButtonId << 6);

      // Status
      message[12] = RepeatCounter;
      message[12] |= 0x10; // N-message

      if (SwitchType == SwitchType.Type1)
      { // Type
        message[12] &= 0xDF;
      }
      else if (SwitchType == SwitchType.Type2)
      {
        message[12] |= 0x20;
      }

      message[12] &= 0x3F; // Reserved
    }
  }
}
